﻿using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

using BEGDC.Utility.Castle;
using BEGDC.Utility.ExceptionManagement;
using BEGDC.CC.BL.Common.Query;
using BEGDC.CC.BL.Workflow;
using BEGDC.CC.BL.UserManagement;
using BEGDC.CC.BL.Common;
using BEGDC.CC.Entity;
using BEGDC.CC.Web.UserControls;
using NetBpm.Util.Client;
using NetBpm.Workflow.Execution;
using NetBpm.Workflow.Execution.EComp;

namespace BEGDC.CC.Web
{
	/// <summary>
	/// Summary description for main.
	/// </summary>
	public class TaskTransfer : System.Web.UI.Page
	{
		protected System.Web.UI.WebControls.ImageButton btnSave;
		protected BEGDC.CC.Web.Control.UserSearchControl SearchStaff;
		protected System.Web.UI.WebControls.DataGrid dgTaskList;
	
		private void Page_Load(object sender, System.EventArgs e)
		{
			try
			{
				// Put user code to initialize the page here
				if(!Page.IsPostBack)
				{
					this.BindData(1);
					this.btnSave.Attributes["onclick"] = "javascript:OnTransfer();";
					
				}
			}
			catch(GDCException gdcEx)
			{
				WebUtility.Go2ErrorPage(this.Page, gdcEx.ErrorNumber);
			}
			catch(Exception ex)
			{
				string s = ex.Message;
				WebUtility.Go2ErrorPage(this.Page, BEGDC.CC.BL.Common.ErrorCode.Other_Exception);
			}
		}

		private void BindData(int pageIndex)
		{
			string employeeno = Request.QueryString["id"];
			IQuery iquery = AppContainer.Instance[ServiceConst.QueryService] as IQuery;
			System.Data.DataTable dtTasks = null;
			
			dtTasks = iquery.QueryTransferTaskList(employeeno);
			int count = dtTasks.Rows.Count;
			
			this.dgTaskList.DataSource = dtTasks;
			this.dgTaskList.DataBind();

		}




		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{    
			this.btnSave.Click += new System.Web.UI.ImageClickEventHandler(this.btnSave_Click);
			this.Load += new System.EventHandler(this.Page_Load);

		}
		#endregion


		private void btnSave_Click(object sender, System.Web.UI.ImageClickEventArgs e)
		{

			string transferTo = this.SearchStaff.EmployeeID.ToString();

			if (transferTo == string.Empty)
			{
				Response.Write(@"<Script>alert('Input employee please.');</Script>");
				return;
			}
			
			string appIDs = string.Empty;
			foreach(DataGridItem item in this.dgTaskList.Items)
			{
				CheckBox chk = item.FindControl("chkSelection") as CheckBox;
				if( chk != null && chk.Checked )
				{
					appIDs += item.Cells[2].Text+",";
				}
			}
			if (appIDs == string.Empty)
			{	
				Page.RegisterClientScriptBlock("","<script>alert('There are no selected application, transfer failed!');</script>");
				return;
			}
			appIDs = appIDs.TrimEnd(',');

			string employeeno = Request.QueryString["id"];
			BEGDC.CC.BL.UserManagement.IUser iuser = AppContainer.Instance[BEGDC.CC.BL.Common.ServiceConst.UserService] as BEGDC.CC.BL.UserManagement.IUser;
			if (iuser.TransferTask(employeeno, transferTo, appIDs))
			{
				Page.RegisterClientScriptBlock("","<script>alert('successfully!');</script>");
			
				// Write Log
				IUtility utility = AppContainer.Instance[ServiceConst.UtilityService] as IUtility;
				CCIdentity id = this.User.Identity as CCIdentity;
				BEGDC.CC.Entity.OperationLog log = new BEGDC.CC.Entity.OperationLog();
				log.Module = "UserManagement";
				log.OperationType = "Transfer Task";
				Employee emp = new Employee();
				emp.Id = id.UserInfo.Id;
				log.Employee = emp;
				log.Time = System.DateTime.Now;
				log.Description = "Transfert task: " + employeeno + " To " + transferTo;
				utility.SaveOperationLog(log);
			}
			else
				Page.RegisterClientScriptBlock("","<script>alert('failed!');</script>");

			this.BindData(1);
		}

	}
}
